package com.alibaba.nacos.naming.utils;

import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Configuration
public class KafkaUtil {
    private static final Logger log = LoggerFactory.getLogger(KafkaUtil.class);

    @Resource(name = "myKafkaProducer")
    private Producer<String, String> producer;

    @Value("${kafka.nacos.topic}")
    private String nacosTopic;

    @Value("${kafka.nacos.service-topic}")
    private String serviceCountTopic;

    @Async
    public void sendNacosMsg( String msg){
        try{
            ProducerRecord<String,String> proRecord = new ProducerRecord<>(nacosTopic,null, msg);
            producer.send(proRecord);
        }catch (Exception e){
            log.error(e.getMessage(), e);
        }
    }

    @Async
    public void sendServiceCountMsg(String msg){
        try{
            ProducerRecord<String,String> myRecord = new ProducerRecord<>(serviceCountTopic,null, msg);
            producer.send(myRecord);
        }catch (Exception e){
            log.error(e.getMessage(), e);
        }
    }
}
